package Interface.Insumos.Convenio.DePara;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import sft.BancodeDados.CriaConexao;
import sft.Logica.Insumos;

public class DeParaDao {
    
    private Connection conexao;
    public DeParaDao() throws SQLException {
        this.conexao = new CriaConexao().getConnection();
    }
    
    public void Adicionar(Insumos p) throws SQLException{
        p.setSql("INSERT INTO sft.sft_convenios (conv_cod_cod, conv_nm_nome, fk_conv) values (?, ?, ?)");
        Operacao("adicionar", p);
    }
    public void Editar(Insumos p) throws SQLException{
        p.setSql("UPDATE sft.sft_convenios SET `conv_cod_cod` =?, `conv_nm_nome` =?,`fk_conv` =? WHERE conv_id like ?");
        Operacao("editar", p);
    }
    public void Deletar(Insumos p) throws SQLException{
        p.setSql("DELETE FROM sft.sft_convenios WHERE conv_id like ?");
        Operacao("deletar", p);
    }

    private void Operacao(String caso, Insumos p) throws SQLException {
        try (PreparedStatement stmt = conexao.prepareStatement(p.getSql())) {
            switch(caso){
                case "adicionar":
                    setSMTP(stmt, p);
                    break;
                    
                case "editar":
                    setSMTP(stmt, p);
                    stmt.setInt(4, p.getId());
                    break;
                    
                case "deletar":
                    stmt.setInt(1, p.getId());
                    break;                
            }                
            stmt.execute();
            stmt.close();
            conexao.close();
        }
    }

    private void setSMTP(PreparedStatement stmt, Insumos p) throws SQLException {
        stmt.setInt(1, p.getCod());
        stmt.setString(2, p.getNome());
        stmt.setInt(3, p.getFk_conv());
    }
}
